<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.DriverManager" %>

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2018/3/20
  Time: 15:46
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>JSP页面</title>
    <style type="text/css">
        table {
            border: 1px solid #000;
            border-collapse: collapse;
        }

        td, th {

            border: 1px solid #000;
            padding: 10px;
        }
    </style>
</head>
<body>
<%--
    out.write()可以向页面输出任何标签
--%>

<%
    //判断用户是否登录
    if (session.getAttribute("isLogin") == null){
        //没有登录，跳转到login.jsp页面
        response.sendRedirect("login.jsp");
    }
%>

<table>
    <thead>
    <tr>
        <td>编号</td>
        <td>用户名</td>
        <td>密码</td>
    </tr>
    </thead>

    <tbody>
    <%

        boolean isLogin = false;
        if (isLogin) {

        }


        class User {
            int id;
            String name;
            String password;

            public User(int id, String name, String password) {
                this.id = id;
                this.name = name;
                this.password = password;
            }
        }

        List<User> list = new ArrayList<User>();
//      list.add(new User(1, "易佳1", "123"));
//      list.add(new User(2, "易佳2", "123"));
//      list.add(new User(3, "易佳3", "123"));
//      list.add(new User(4, "易佳4", "123"));

        //3+1
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;

        String sql = "select * from t_users";

        Class.forName("com.mysql.jdbc.Driver");

        connection = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/sctu_javaweb_2018?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC",
                "root",
                "123456"
        );

        statement = connection.createStatement();
        resultSet = statement.executeQuery(sql);

        while (resultSet.next()) {
            int id = resultSet.getInt(1);
            String userName = resultSet.getString(2);
            String password = resultSet.getString(3);

            User user = new User(id, userName, password);

            list.add(user);
        }


        //遍历List
        for (User user : list) {
            out.write("<tr><td>" + user.id + "</td><td>" + user.name + "</td><td>" + user.password + "</td></tr>");
        }

        resultSet.close();
        statement.close();
        connection.close();

//      for (int i = 0; i < 10; i++) {
//
//        out.write("<tr><td>" + (i + 1) + "</td><td>易佳</td><td>1212122</td></tr>");
//
//      }

    %>
    </tbody>
</table>
<button><a href="userList.jsp">查看用户列表</a></button>

<%--
JSP指令：包含静态的信息
page
include
taglib
--%>

<%--
<%@include file="footer.jsp"%>
--%>

<%--
JSP动作元素：包含动态的信息
JSP前缀
--%>
<jsp:include page="footer.jsp">
    <jsp:param name="year" value="2018"/>
</jsp:include>
</body>
</html>
